<template>
  <div class="app-container">
    <el-row :gutter="10" style="position: relative; margin-bottom: 5px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:materiel:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:materiel:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:materiel:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:materiel:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :loading="exportLoading"
          @click="handleAddCsm(null)"
          :disabled="multiple"
          v-hasPermi="['project:itcsl:add']"
        >
          批量添加厂商
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :loading="exportLoading"
          @click="editWorkshop()"
          :disabled="multipleOnly"
          v-hasPermi="['project:materiel:workshop']"
        >
          修改车间
        </el-button>
      </el-col>
      <el-col :span="12.5" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      ref="multipleTable"
      border
      highlight-current-row
      :height="tableH"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      :data="materielList"
      @row-click="getChildList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column fixed type="selection" align="center" />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="itmWldm"
      />
      <!-- <el-table-column label="单价" :show-overflow-tooltip="true" width="150" align="center" prop="itmPrice" /> -->
      <el-table-column
        label="物品品名"
        :show-overflow-tooltip="true"
        width="110"
        prop="itmWlpm"
      />
      <el-table-column
        label="物料规格"
        :show-overflow-tooltip="true"
        width="330"
        prop="itmWlgg"
      />
      <el-table-column
        label="材质"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmCzdm"
      />
      <el-table-column
        label="颜色"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmYsdm"
      />
      <el-table-column
        label="毛重"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmMz"
      />
      <el-table-column
        label="净重"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmJz"
      />
      <el-table-column
        label="单位水口"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmSkzl"
      />
      <el-table-column label="制造车间" width="80" align="center" prop="itmCj">
        <template #default="scope">
          <dict-tag :options="itCarOptions" :value="scope.row.itmCj" />
        </template>
      </el-table-column>
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="itmDw"
      >
        <template #default="scope">
          <dict-tag :options="itmDwAllOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="itmCustwldm"
      />
      <el-table-column
        label="图纸编号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="itmTzbh"
      />
      <el-table-column
        label="最小包装"
        width="80"
        align="center"
        prop="itmMinbz"
      />
      <el-table-column
        label="仓库代码"
        width="80"
        align="center"
        prop="itmCkdmName"
      />
      <el-table-column
        label="仓位代码"
        width="100"
        align="center"
        prop="itmCwdmName"
      />

      <el-table-column
        label="第二仓库"
        width="80"
        align="center"
        prop="itmCkdmTwo"
      >
        <template #default="scope">
          <dict-tag
            :options="efficientStorageLists"
            :value="scope.row.itmCkdmTwo"
            label-key="storageName"
            value-key="storageId"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="第二仓位"
        width="100"
        align="center"
        prop="itmCwdmTwo"
      >
        <template #default="scope">
          <dict-tag
            :options="efficientStorageLists"
            :value="scope.row.itmCwdmTwo"
            label-key="storageDefaultstorage"
            value-key="storageId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="350"
        prop="itmPmgg"
      />
      <el-table-column
        label="其他信息"
        :show-overflow-tooltip="true"
        width="180"
        align="center"
        prop="itmQtinf"
      />
      <el-table-column
        label="机种代码"
        width="130"
        align="center"
        :show-overflow-tooltip="true"
        prop="itmJzdm"
      />
      <el-table-column
        label="尺寸描述"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmCcms"
      />
      <el-table-column
        label="报价周期"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmEdzq"
      />
      <el-table-column
        label="检测尺寸1"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc1"
      />
      <el-table-column
        label="检测尺寸2"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc2"
      />
      <el-table-column
        label="检测尺寸3"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc3"
      />
      <el-table-column
        label="检测重量"
        width="80"
        align="center"
        prop="itmJczl"
      />
      <el-table-column
        label="建立人员"
        width="80"
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="建立日期"
        align="center"
        prop="createTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        width="80"
        align="center"
        prop="updateBy"
      />
      <el-table-column
        label="更改日期"
        align="center"
        prop="updateTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
        width="300"
      >
        <template #default="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleAddCsm(scope.row)"
            v-hasPermi="['project:itcsl:add']"
          >
            添加厂商
          </el-button>

          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:materiel:edit']"
          >
            修改
          </el-button>

          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:materiel:remove']"
          >
            删除
          </el-button>
          <el-dropdown
            size="small"
            @command="(command) => handleCommand(command, scope.row)"
            v-hasPermi="['project:materiel:edit']"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>
              更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleLookFile"
                icon="el-icon-reading"
                v-hasPermi="['project:materiel:lookFile']"
                >查看文档</el-dropdown-item
              >
              <el-dropdown-item
                command="handleUploadFile"
                icon="el-icon-upload"
                v-hasPermi="['project:materiel:uploadFile']"
              >
                上传文档
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!-- 子项Table   -->
    <el-table
      border
      stripe
      highlight-current-row
      :height="tableHChild"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      :data="csmList"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="厂商代码"
        width="150"
        align="center"
        prop="itcslCsdm"
      />
      <el-table-column
        label="厂商名称"
        width="150"
        align="center"
        prop="itcslCsmc"
      />
      <el-table-column
        label="供货比例"
        width="150"
        align="center"
        prop="supplyQuota"
      />
      <el-table-column label="有效标识" align="center" width="100">
        <template #default="scope">
          <el-switch
            v-model="scope.row.itcslFlag"
            active-value="0"
            inactive-value="1"
            v-hasPermi="['project:itcsl:updateItcs']"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!--      <el-table-column label="最近采购单" width="100" align="center" prop="itcslZjdh"/>-->
      <el-table-column label="备注" width="150" align="center" prop="remark" />
      <el-table-column
        label="建立人员"
        width="80"
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="建立日期"
        align="center"
        prop="createTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        width="80"
        align="center"
        prop="updateBy"
      />
      <el-table-column
        label="更改日期"
        align="center"
        prop="updateTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="left" align="center" width="100">
        <template #default="scope">
          <!--          <el-button size="mini" type="text" @click="handleUpdateCsm(scope.row)" v-hasPermi="['project:itcsl:edit']">修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['project:itcsl:remove']"
            @click="handleDeleteCsm(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label-width="125px" label="仓库代码" prop="itmCkdm">
            <el-select
              v-model="queryParams.itmCkdm"
              style="width: 400px"
              placeholder="请输入仓库代码"
              clearable
            >
              <el-option
                v-for="dict in storageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="totalOrder">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.totalOrder"
              placeholder="请输入物料代码或品名规格或客户编码"
              clearable
              style="width: 400px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" label="建立日期" prop="createTime">
            <el-date-picker
              clearable
              size="small"
              v-model="queryParams.createTimes"
              :picker-options="pickerOptions"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              :clearable="false"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="display: flex; margin-top: 20px; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 添加生产物料信息对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="880px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="height: 500px"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="客户" prop="cusId">
              <el-select
                v-model="form.cusId"
                placeholder="请选择客户"
                @change="selectBigLbm"
              >
                <el-option
                  v-for="dict in cus"
                  :key="dict.cusId"
                  :label="dict.cusCode + dict.cusName"
                  :value="{ value: dict.cusId, label: dict.cusCode }"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="大分类" prop="bigLbmId">
              <el-select
                v-model="form.bigLbmId"
                placeholder="请选择大分类"
                @change="selectMiddleLbm"
              >
                <el-option
                  v-for="dict in bigLbm"
                  :key="dict.lbmId"
                  :label="dict.lbmLbdm + dict.lbmLbmc"
                  :value="{ value: dict.lbmId, label: dict.lbmLbdm }"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="中分类" prop="middleLbmId">
              <el-select
                v-model="form.middleLbmId"
                placeholder="请选择中分类"
                @change="selectSmallLbm"
              >
                <el-option
                  v-for="dict in middleLbm"
                  :key="dict.lbmId"
                  :label="dict.lbmLbdm + dict.lbmLbmc"
                  :value="{ value: dict.lbmId, label: dict.lbmLbdm }"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="小分类" prop="smallLbmId">
              <el-select
                v-model="form.smallLbmId"
                placeholder="请选择小分类"
                @change="generate"
              >
                <el-option
                  v-for="dict in smallLbm"
                  :key="dict.lbmId"
                  :label="dict.lbmLbdm + dict.lbmLbmc"
                  :value="{ value: dict.lbmId, label: dict.lbmLbdm }"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="itmWldm">
            <el-input v-model="form.itmWldm" disabled />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="单价" prop="itmWldm">
            <el-input v-model="form.itmPrice" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12" class="right">
          <el-form-item label="物品品名" prop="itmWlpm">
            <el-input v-model="form.itmWlpm" placeholder="请输入物品品名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材质" prop="itmCzdm">
            <el-input
              v-model="form.itmCzdm"
              placeholder="请输入材质"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="right">
          <el-form-item label="颜色" prop="itmYsdm">
            <el-input
              v-model="form.itmYsdm"
              placeholder="请输入颜色"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="净重" prop="itmJz">
            <el-input v-model="form.itmJz" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="unit-parent">
          <span class="unit">kg</span>
        </el-col>
        <el-col :span="5">
          <el-form-item label="报价周期" prop="itmEdzq">
            <el-input v-model="form.itmEdzq" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="unit-parent">
          <span class="unit">s</span>
        </el-col>
        <el-col :span="5">
          <el-form-item label="单位水口" prop="itmSkzl">
            <el-input v-model="form.itmSkzl" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="unit-parent">
          <span class="unit">kg</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单位" prop="itmDw">
            <el-select v-model="form.itmDw" placeholder="请选择单位">
              <el-option
                v-for="dict in itmDwOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="尺寸描述" prop="itmCcms">
            <el-input
              v-model="form.itmCcms"
              placeholder="请输入尺寸描述"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="right">
          <el-form-item label="客户编码" prop="itmCustwldm">
            <el-input
              v-model="form.itmCustwldm"
              placeholder="请输入客户编码"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图纸编号" prop="itmTzbh">
            <el-input v-model="form.itmTzbh" placeholder="请输入图纸编号" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓库代码" prop="itmCkdm">
            <el-select v-model="form.itmCkdm" placeholder="请输入仓库代码">
              <el-option
                v-for="dict in storageList"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓位代码" prop="itmCwdm">
            <el-select v-model="form.itmCwdm" placeholder="请输入仓位代码">
              <el-option
                v-for="dict in storageList"
                :key="dict.storageId"
                :label="dict.storageDefaultstorage"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="第二仓库" prop="itmCkdmTwo">
            <el-select
              clearable
              v-model="form.itmCkdmTwo"
              placeholder="请输入仓库代码"
              @change="
                form.itmCwdmTwo = form.itmCkdmTwo;
                form.itmKwdmTwo = form.itmCkdmTwo;
              "
            >
              <el-option
                v-for="dict in efficientStorageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="第二仓位" prop="itmCwdmTwo">
            <el-select
              v-model="form.itmCwdmTwo"
              placeholder="请输入仓位代码"
              disabled
            >
              <el-option
                v-for="dict in efficientStorageLists"
                :key="dict.storageId"
                :label="dict.storageDefaultstorage"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="机种代码" prop="itmJzdm">
            <!--            <el-input v-model="form.itmJzdm" placeholder="请输入机种代码"/>-->
            <el-select v-model="form.itmJzdm" placeholder="请选择机种代码">
              <el-option
                v-for="dict in jzmList"
                :key="dict.jzmId"
                :label="dict.jzmJzdm"
                :value="dict.jzmJzdm"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="制造车间" prop="itmCj">
            <!--            <el-input v-model="form.itmCj" placeholder="请输入制造车间"/>-->
            <el-select v-model="form.itmCj" placeholder="请选择制造车间">
              <el-option
                v-for="dict in itCarOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="最小包装" prop="itmMinbz">
            <el-input
              v-model="form.itmMinbz"
              type="number"
              @input="handleInput"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="标准包装1" prop="itmBzsl1">
            <el-input v-model="form.itmBzsl1" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="18" class="right">
          <el-form-item label="其他信息" prop="itmQtinf">
            <el-input
              v-model="form.itmQtinf"
              placeholder="请输入其他信息"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="标准包装2" prop="itmBzsl2">
            <el-input v-model="form.itmBzsl2" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料规格" prop="itmWlgg">
            <el-input v-model="form.itmWlgg" placeholder="请输入物料规格" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改生产物料信息对话框 -->
    <el-dialog
      :title="title"
      v-model="openStr"
      :close-on-click-modal="false"
      width="881px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="height: 500px"
      >
        <el-col :span="12">
          <el-form-item label="物料代码" prop="itmWldm">
            <el-input v-model="form.itmWldm" disabled />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="单价" prop="itmPrice">
            <el-input v-model="form.itmPrice" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12" class="right">
          <el-form-item label="物品品名" prop="itmWlpm">
            <el-input v-model="form.itmWlpm" placeholder="请输入物品品名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材质" prop="itmCzdm">
            <el-input
              v-model="form.itmCzdm"
              placeholder="请输入材质"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="right">
          <el-form-item label="颜色" prop="itmYsdm">
            <el-input
              v-model="form.itmYsdm"
              placeholder="请输入颜色"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="尺寸描述" prop="itmCcms">
            <el-input
              v-model="form.itmCcms"
              placeholder="请输入尺寸描述"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="净重" prop="itmJz">
            <el-input v-model="form.itmJz" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="unit-parent">
          <span class="unit">kg</span>
        </el-col>
        <el-col :span="8">
          <el-form-item label="报价周期" prop="itmEdzq">
            <el-input v-model="form.itmEdzq" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="3" class="unit-parent">
          <div style="display: flex">
            <span class="unit">s</span>
            <span class="unit-button">
              <el-button size="mini" @click="changeClick">变更</el-button>
            </span>
          </div>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单位水口" prop="itmSkzl">
            <el-input v-model="form.itmSkzl" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="unit-parent">
          <span class="unit">kg</span>
        </el-col>
        <el-col :span="5">
          <el-form-item label="单位" prop="itmDw">
            <el-select v-model="form.itmDw" placeholder="请选择单位">
              <el-option
                v-for="dict in itmDwOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1" class="unit-parent">
          <span class="unit"></span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="第二仓库" prop="itmCkdmTwo">
            <el-select
              clearable
              v-model="form.itmCkdmTwo"
              placeholder="请输入仓库代码"
              @change="
                form.itmCwdmTwo = form.itmCkdmTwo;
                form.itmKwdmTwo = form.itmCkdmTwo;
              "
            >
              <el-option
                v-for="dict in efficientStorageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="第二仓位" prop="itmCwdmTwo">
            <el-select
              v-model="form.itmCwdmTwo"
              placeholder="请输入仓位代码"
              disabled
            >
              <el-option
                v-for="dict in efficientStorageLists"
                :key="dict.storageId"
                :label="dict.storageDefaultstorage"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" class="right">
          <el-form-item label="客户编码" prop="itmCustwldm">
            <el-input
              v-model="form.itmCustwldm"
              placeholder="请输入客户编码"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="图纸编号" prop="itmTzbh">
            <el-input v-model="form.itmTzbh" placeholder="请输入图纸编号" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="机种代码" prop="itmJzdm">
            <!--            <el-input v-model="form.itmJzdm" placeholder="请输入机种代码"/>-->
            <el-select v-model="form.itmJzdm" placeholder="请选择机种代码">
              <el-option
                v-for="dict in jzmList"
                :key="dict.jzmId"
                :label="dict.jzmJzdm"
                :value="dict.jzmJzdm"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="制造车间" prop="itmCj">
            <!--            <el-input v-model="form.itmCj" placeholder="请输入制造车间"/>-->
            <el-select v-model="form.itmCj" placeholder="请选择制造车间">
              <el-option
                v-for="dict in itCarOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="最小包装" prop="itmMinbz">
            <el-input type="number" v-model="form.itmMinbz" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="标准包装1" prop="itmBzsl1">
            <el-input v-model="form.itmBzsl1" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="标准包装2" prop="itmBzsl2">
            <el-input v-model="form.itmBzsl2" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="18" class="right">
          <el-form-item label="其他信息" prop="itmQtinf">
            <el-input
              v-model="form.itmQtinf"
              placeholder="请输入其他信息"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料规格" prop="itmWlgg">
            <el-input
              v-model="form.itmWlgg"
              placeholder="请输入物料规格"
              @input="getContainer"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelStr">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!--  添加厂商对话框  -->
    <el-dialog
      :title="title"
      v-model="addCsmView"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        ref="csmForm"
        :model="csmForm"
        :rules="csmFormRules"
        label-width="80px"
      >
        <el-form-item label="厂商名称" prop="itcslCsmc">
          <el-select
            class="inline-input"
            multiple
            filterable
            clearable
            placeholder="请输入关键词查询"
            v-model="csmForm.itcslCs"
            style="width: 530px"
            :value-key="'csmCsmc'"
            @change="handleSelect"
          >
            <el-option
              v-for="item in allCsmData"
              :key="item.csmCsdm"
              :label="item.csmCsmc"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="厂商代码" prop="itcslCsdm">
          <el-input v-model="csmForm.itcslCsdm" disabled />
        </el-form-item>
        <el-form-item label="供货配额" prop="supplyQuota">
          <el-input
            type="number"
            min="0"
            max="1"
            @keyup.native="proving($event)"
            @input="editInput($event, 'supplyQuota')"
            oninput="if(value>1)value=1;if(value<0)value=0"
            v-model="csmForm.supplyQuota"
          />
        </el-form-item>
        <el-form-item label="物料代码" prop="itmWldm">
          <el-input v-model="csmForm.itcslWldm" disabled />
        </el-form-item>
        <el-form-item label="品名规格" prop="itmPmgg">
          <el-input v-model="csmForm.itmPmgg" disabled />
        </el-form-item>
        <el-form-item label="有效标志">
          <el-radio-group v-model="csmForm.itcslFlag">
            <el-radio
              v-for="dict in csmWldmFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="csmForm.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelCsm">取 消</el-button>
        <el-button type="primary" @click="submitCsmForm">确 定</el-button>
      </div>
    </el-dialog>

    <!--物料文档对话框-->
    <el-dialog
      :title="title"
      v-model="openFile"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料代码" prop="itmWldm">
          <el-input v-model="form.itmWldm" disabled />
        </el-form-item>
        <el-form-item label="品名" prop="itmPmgg">
          <el-input v-model="form.itmPmgg" disabled />
        </el-form-item>
      </el-form>
      <div style="margin-top: 50px; width: 100%; text-align: right">
        <el-upload
          action="#"
          :show-file-list="false"
          :before-upload="beforeUpload"
          accept=".pdf"
        >
          <el-button size="small">
            上传
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </el-upload>
      </div>
    </el-dialog>
    <el-dialog
      title="修改制造车间"
      v-model="workShop.open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <span>制造车间：</span>
      <el-select v-model="workShop.form.itmCj" placeholder="请选择制造车间">
        <el-option
          v-for="dict in itCarOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
        ></el-option>
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button @click="workShop.open = false">取 消</el-button>
        <el-button type="primary" @click="submitWorkShop">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMateriel,
  getMateriel,
  delMateriel,
  addMateriel,
  updateMateriel,
  exportMateriel,
  getFirstChild,
  generateItemCode,
  getStorage,
  changeStorage,
  updateItcs,
  updateWorkShop,
} from "@/api/project/materiel";
import { getAllCustomer } from "@/api/project/customer";
import { getAreaTree } from "@/api/project/lbm";
import { getAllJzm } from "@/api/project/jzmMstr";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getCs } from "@/api/purchase/csm";
import {
  bindCsm,
  deleteCsm,
  getCsmByWldm,
  updateCsm,
} from "../../../api/project/materiel";
import { uploadImg } from "@/api/project/iqmMstr";
import { Split } from "view-design";

export default {
  name: "Materiel",
  data() {
    return {
      materrial_prefix: "",
      //物料Id
      itmMstr: null,
      //客户ID
      cusId: null,
      //客户分类
      cus: [],
      //大分类信息
      bigLbm: [],
      //中分类
      middleLbm: [],
      //小分类
      smallLbm: [],
      //仓位信息
      storageList: [],

      //当前物料代码
      itmData: null,

      //添加厂商对话框
      addCsmView: false,

      //所有厂商
      allCsmData: [],

      //物料厂商列表
      csmList: [],
      //仓库信息
      storageLists: [],
      //有效仓库信息
      efficientStorageLists: [],
      //有效仓位信息
      efficientPositionLists: [],
      //机种信息
      jzmList: [],
      //分类Form
      lbmForm: {},
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight / 2 - 82,
      tableHChild: document.body.clientHeight / 2 - 100,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],

      timer: null,
      itmImg: null,

      //选中的物料名称
      itmWldms: [],
      selection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      multipleOnly: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产物料信息表格数据
      materielList: [],
      // 弹出层标题
      title: "",
      // 是否弹出新增对话框
      open: false,
      openSearch: false,
      //是否弹出修改对话框
      openStr: false,
      //上传文件对话框
      openFile: false,
      // 单位字典
      itmDwOptions: [],

      //所有单位字典
      itmDwAllOptions: [],
      //制造车间字典
      itCarOptions: [],
      //有效标识
      csmWldmFlagOptions: [],
      // 查询参数
      queryParams: {
        totalOrder: null,
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 14 : 10,
        itmWldm: null,
        itmPmgg: null,
        itmCustwldm: null,
        itmCkdm: null,
        createBy: null,
        createTimes: null,
        updateBy: null,
        updateTime: null,
        beginDate: null,
        endDate: null,
      },
      // 表单参数
      form: {
        itmId: null,
        itmWldm: null,
        itmPrice: null,
        itmWlpm: null,
        itmWlgg: null,
        itmPmgg: null,
        itmCzdm: null,
        itmYsdm: null,
        itmQtinf: null,
        itmMz: null,
        itmJz: 0,
        itmSkzl: 0,
        itmDw: null,
        itmCustwldm: null,
        itmTzbh: null,
        itmCgsx: null,
        itmMinbz: 0,
        itmBzsl1: 0,
        itmBzsl2: 0,
        itmCwdm: null,
        itmCkdm: null,
        itmKwdm: null,
        itmCwdmTwo: null,
        itmCkdmTwo: null,
        itmKwdmTwo: null,
        itmJzdm: null,
        itmCcms: null,
        itmZtbz: null,
        itmEdzq: 0,
        itmTedzp: null,
        itmShry: null,
        itmShrq: null,
        itmPzry: null,
        itmPzrq: null,
        itmJccc1: null,
        itmJccc2: null,
        itmJccc3: null,
        itmJczl: null,
        itmCj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        cusId: null,
        bigLbmId: null,
        middleLbmId: null,
        smallLbmId: null,
        cusCode: null,
        bigLbmCode: null,
        middleLbmCode: null,
        smallLbmCode: null,
      },
      csmForm: {},
      // 表单校验
      rules: {
        itmWlpm: [
          { required: true, message: "物品品名不能为空", trigger: "blur" },
        ],
        itmDw: [{ required: true, message: "单位不能为空", trigger: "change" }],
        itmMinbz: [
          { required: true, message: "最小包装不能为空", trigger: "blur" },
        ],
        itmCwdm: [
          { required: true, message: "仓位代码不能为空", trigger: "blur" },
        ],
        itmCkdm: [
          { required: true, message: "仓库代码不能为空", trigger: "blur" },
        ],
        cusId: [{ required: true, message: "客户不能为空", trigger: "change" }],
        bigLbmId: [
          { required: true, message: "大分类不能为空", trigger: "change" },
        ],
        middleLbmId: [
          { required: true, message: "中分类不能为空", trigger: "change" },
        ],
      },
      csmFormRules: {
        itcslCsdm: [
          { required: true, message: "厂商代码不能为空", trigger: "blur" },
        ],
        supplyQuota: [
          { required: true, message: "供货配额不能为空", trigger: "blur" },
        ],
      },

      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      workShop: {
        open: false,
        form: {},
      },
    };
  },
  created() {
    this.getConfigKey("sys.materrial.prefix").then((response) => {
      this.materrial_prefix = response.msg;
    });
    this.getList();
    this.getDicts("sys_unit").then((response) => {
      this.itmDwOptions = response.data;
    });

    this.getAllDicts("sys_unit").then((response) => {
      this.itmDwAllOptions = response.data;
    });

    this.getDicts("sys_flag").then((response) => {
      this.csmWldmFlagOptions = response.data;
    });
    //制造车间字典
    this.getDicts("Manufacturing_plant").then((response) => {
      this.itCarOptions = response.data;
    });

    getAll().then((response) => {
      this.storageLists = response.data;
    });
    getstorageType(0).then((response) => {
      this.efficientStorageLists = response.data.map((a) => {
        a.listClass = "default";
        a.storageId = "" + a.storageId;
        return a;
      });
    });
  },
  methods: {
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产物料信息";
      this.openSearch = true;
    },
    /** 查询生产物料信息列表 */
    getList(row) {
      this.loading = true;
      listMateriel(this.queryParams).then((response) => {
        this.materielList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (row) {
          this.getChildList(row);
        } else if (this.materielList.length > 0) {
          this.getChildList(this.materielList[0]);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    cancelStr() {
      this.openStr = false;
      this.reset();
    },
    cancelCsm() {
      this.addCsmView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itmId: null,
        itmWldm: null,
        itmPrice: null,
        itmWlpm: null,
        itmWlgg: null,
        itmPmgg: null,
        itmCzdm: null,
        itmYsdm: null,
        itmQtinf: null,
        itmMz: null,
        itmJz: 0,
        itmSkzl: 0,
        itmDw: null,
        itmCustwldm: null,
        itmTzbh: null,
        itmCgsx: null,
        itmMinbz: 0,
        itmBzsl1: 0,
        itmBzsl2: 0,
        itmCwdm: null,
        itmCkdm: null,
        itmKwdm: null,
        itmCwdmTwo: null,
        itmCkdmTwo: null,
        itmKwdmTwo: null,
        itmJzdm: null,
        itmCcms: null,
        itmZtbz: null,
        itmEdzq: 0,
        itmTedzp: null,
        itmShry: null,
        itmShrq: null,
        itmPzry: null,
        itmPzrq: null,
        itmJccc1: null,
        itmJccc2: null,
        itmJccc3: null,
        itmJczl: null,
        itmCj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        cusId: null,
        bigLbmId: null,
        middleLbmId: null,
        smallLbmId: null,
        cusCode: null,
        bigLbmCode: null,
        middleLbmCode: null,
        smallLbmCode: null,
      };

      this.csmForm = {
        itcslId: null,
        itcslCsdm: null,
        itcslCsmc: null,
        supplyQuota: null,
        itcslWldm: null,
        itmPmgg: null,
        remark: null,
        itcslFlag: "0",
      };

      this.resetForm("csmForm");

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.createTimes != null) {
        this.queryParams.beginDate = this.queryParams.createTimes[0];
        this.queryParams.endDate = this.queryParams.createTimes[1];
      }
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
      this.ids = selection.map((item) => item.itmId);
      this.itmWldms = selection.map((item) => item.itmWldm);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.multipleOnly = selection.length == 1 ? false : true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      getAllCustomer().then((response) => {
        this.cus = response.data;
        getAllJzm().then((res) => {
          this.jzmList = res.data;
        });
        this.reset();
        this.open = true;
        this.title = "添加生产物料信息";
      });
    },
    getContainer() {
      this.form.itmWlgg =
        (this.form.itmCustwldm ? this.form.itmCustwldm + "," : "") +
        (this.form.itmCzdm ? this.form.itmCzdm + "," : "") +
        (this.form.itmYsdm ? this.form.itmYsdm + "," : "") +
        (this.form.itmCcms ? this.form.itmCcms + "," : "") +
        (this.form.itmQtinf ? this.form.itmQtinf + "," : "");
      this.form.itmWlgg = this.form.itmWlgg.slice(
        0,
        this.form.itmWlgg.length - 1
      );
    },

    /** 查询 大分类 */
    selectBigLbm(data) {
      //查询大分类的话
      this.form.bigLbmId = null;
      this.form.middleLbmId = null;
      this.form.smallLbmId = null;
      this.form.itmCkdm = null;
      this.form.itmCwdm = null;
      this.form.itmWldm = null;
      this.bigLbm = [];
      this.middleLbm = [];
      this.smallLbm = [];
      this.storageList = [];

      getFirstChild(data.value).then((response) => {
        if (response.data.length < 1) {
          this.msgError("无大分类，请前往客户代码分配大分类，否则无法添加物料");

          return;
        }

        this.form.cusCode = data.label;

        this.bigLbm = response.data;
      });
    },

    /** 查询中分类 */
    selectMiddleLbm(data) {
      this.form.middleLbmId = null;
      this.form.smallLbmId = null;
      this.form.itmWldm = null;
      this.middleLbm = [];
      this.smallLbm = [];
      this.form.itmCkdm = null;
      this.form.itmCwdm = null;
      this.storageList = [];

      getAreaTree(data.value).then((response) => {
        if (response.data.length < 1) {
          this.msgError(
            "无中分类，请前往类别代码对应大分类中添加中分类，否则无法添加物料"
          );

          return;
        }
        this.form.bigLbmCode = data.label;

        this.middleLbm = response.data;
      });
    },

    /** 查询小分类 */
    selectSmallLbm(data) {
      this.form.itmWldm = null;
      getAreaTree(data.value).then((response) => {
        if (response.data.length < 1) {
          //TODO 直接生成物料代码
          //开始构建参数
          let wldm =
            this.materrial_prefix +
            "." +
            this.form.cusCode +
            "." +
            this.form.bigLbmCode +
            "." +
            data.label +
            "." +
            "00" +
            ".";
          //向后端发送请求查询最新的对应的物料代码
          generateItemCode(wldm).then((response) => {
            this.form.itmWldm = response.msg;
          });
        }

        this.middleLbmCode = data.label;

        this.smallLbm = response.data;
      });

      //TODO 查询对应的仓位以及储位   如果后期采用仓储系统，这个地方查询的就不是这个接口
      let ppwldm = this.form.cusCode + "." + this.form.bigLbmCode;
      // 荆州定制需求
      const regStr = `${this.form.cusCode}.${this.form.bigLbmCode}.${data.label}`;
      let rulesReg = [];
      if (this.materrial_prefix === "J") {
        rulesReg = [
          {
            reg: /^.*?\.6\..*/,
            sort: 10,
            storage: [
              {
                storageCode: "FL",
                storageDefaultstorage: "FL-000-00-00",
                storageFlag: null,
                storageId: 7,
                storageName: "辅料仓",
              },
            ],
          },
          {
            reg: /^.*?\.6\.(01|12)/,
            sort: 50,
            storage: [
              {
                storageCode: "ZX",
                storageDefaultstorage: "ZX-000-00-00",
                storageFlag: null,
                storageId: 17,
                storageName: "纸箱仓",
              },
            ],
          },
          {
            reg: /^.*?\.6\.(02)/,
            sort: 50,
            storage: [
              {
                storageCode: "WJ",
                storageDefaultstorage: "WJ-000-00-00",
                storageFlag: null,
                storageId: 9,
                storageName: "五金仓",
              },
            ],
          },
          {
            reg: /^.*?\.3\.(07|14)/,
            sort: 50,
            storage: [
              {
                storageCode: "DZ",
                storageDefaultstorage: "YL-000-00-00",
                storageFlag: null,
                storageId: 12,
                storageName: "电子仓",
              },
            ],
          },
          {
            reg: /^.*?\.3\.((?!(01|02)).*?)/,
            sort: 60,
            storage: [
              {
                storageCode: "FL",
                storageDefaultstorage: "FL-000-00-00",
                storageFlag: null,
                storageId: 7,
                storageName: "辅料仓",
              },
            ],
          },
        ];
      } else {
        rulesReg = [
          {
            reg: /^.*?\.3\.((?!(01|02)).*?)/,
            sort: 60,
            storage: [
              {
                storageCode: "FL",
                storageDefaultstorage: "FL-000-00-00",
                storageFlag: null,
                storageId: 7,
                storageName: "辅料仓",
              },
            ],
          },
        ];
      }
      const rule = rulesReg
        .filter((o) => o.reg.test(regStr))
        .sort((a, b) => b.sort - a.sort);
      console.log(rule);
      if (rule[0]) {
        this.storageList = rule[0].storage;
        this.form.itmCwdm = this.storageList[0].storageId;
        this.form.itmCkdm = this.storageList[0].storageId;
        this.form.itmKwdm = this.storageList[0].storageId;
        return;
      }

      if (this.form.bigLbmCode == "2") {
        if (["02"].includes(data.label)) {
          this.storageList = [
            {
              storageCode: "PBC",
              storageDefaultstorage: "PBC-000-00-00",
              storageFlag: null,
              storageId: 16,
              storageName: "喷涂半成品仓",
            },
          ];
          this.form.itmCwdm = this.storageList[0].storageId;
          this.form.itmCkdm = this.storageList[0].storageId;
          this.form.itmKwdm = this.storageList[0].storageId;
          return;
        }
      }

      getStorage(ppwldm).then((response) => {
        if (response.data.length < 1) {
          this.msgError(
            "该物料绑定的仓位失效或没有绑定仓位，请前往库存管理/仓位分配绑定仓位,否则无法创建该物料"
          );

          return;
        }
        this.storageList = response.data;
        this.form.itmCwdm = this.storageList[0].storageId;
        this.form.itmCkdm = this.storageList[0].storageId;
        this.form.itmKwdm = this.storageList[0].storageId;
      });
    },

    generate(data) {
      this.form.itmWldm = null;
      //TODO 直接生成物料代码
      //开始构建参数
      let wldm =
        this.materrial_prefix +
        "." +
        this.form.cusCode +
        "." +
        this.form.bigLbmCode +
        "." +
        this.middleLbmCode +
        "." +
        data.label +
        ".";

      //向后端发送请求
      generateItemCode(wldm).then((response) => {
        this.form.itmWldm = response.msg;
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const itmId = row.itmId || this.ids;
      getMateriel(itmId).then((response) => {
        getAllJzm().then((res) => {
          this.jzmList = res.data;
        });
        response.data.itmBzsl = response.data.itmBzsl || "";
        response.data.itmBzsl1 = parseInt(
          response.data.itmBzsl.split(";")[0] || 0
        );
        response.data.itmBzsl2 = parseInt(
          response.data.itmBzsl.split(";")[1] || 0
        );
        this.form = response.data;
        this.openStr = true;
        this.title = "修改生产物料信息";
      });
    },
    changeClick() {
      changeStorage(this.form.itmId, this.form.itmEdzq).then((response) => {
        this.msgSuccess("变更成功");
        this.openStr = false;
      });
    },
    handleInput(a) {
      if (a < 0) {
        this.form.itmMinbz = 0;
      }
    },
    /** 提交按钮 */
    submitForm() {
      // if (this.form.itmDw == 'unit_3' && this.form.itmJz == 0) {
      //   this.msgError('净重不能为0')
      //   return 0
      // }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.itmBzsl = this.form.itmBzsl1 + ";" + this.form.itmBzsl2;
          if (this.form.itmId != null) {
            var number = this.form.itmWldm.match(/\d/)[0];
            if ((number == "1" || number == "2") && this.form.itmCj == null) {
              this.msgError("成品、半成品必须选择制造车间!");
            } else {
              updateMateriel(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.openStr = false;
                this.getList();
              });
            }
          } else {
            if (
              (this.form.bigLbmId.label == "1" ||
                this.form.bigLbmId.label == "2") &&
              this.form.itmCj == null
            ) {
              this.msgError("成品、半成品必须选择制造车间!");
            } else {
              addMateriel(this.form).then((response) => {
                this.msgSuccess("新增成功");
                // this.open = false
                this.form = {
                  ...this.form,
                  itmId: null,
                  itmWldm: null,
                  cusId: null,
                  bigLbmId: null,
                  middleLbmId: null,
                  smallLbmId: null,
                  cusCode: null,
                  bigLbmCode: null,
                  middleLbmCode: null,
                  smallLbmCode: null,
                };
                this.getList();
              });
            }
          }
        }
      });
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleLookFile":
          this.handleLookFile(row);
          break;
        case "handleUploadFile":
          this.handleUploadFile(row);
          break;
        default:
          break;
      }
    },
    // 查看文档
    handleLookFile(row) {
      this.timer = new Date().getTime();
      this.itmImg = row.itmImage
        ? process.env.VUE_APP_BASE_API + row.itmImage
        : null;
    },
    //上传文档
    handleUploadFile(row) {
      this.reset();
      const itmId = row.itmId;
      getMateriel(itmId).then((response) => {
        this.form = response.data;
        this.openFile = true;
        this.title = "物料文档上传";
      });
    },
    beforeUpload(file) {
      if (file.type.indexOf("pdf") == -1) {
        this.msgError("文件格式错误，请上传类型为PDF后缀的文件。");
      } else {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => {
          let formData = new FormData();
          formData.append("avatarfile", file);
          formData.append("wldm", this.form.itmWldm);

          uploadImg(formData).then((response) => {
            this.openImg = false;
            this.msgSuccess("上传文件成功");
            document.body.clientHeight;
            this.itmImg = reader.result;
            this.getList();
            this.openFile = false;
          });
        };
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itmIds = row.itmId || this.ids;
      this.$confirm(
        '是否确认删除生产物料信息编号为"' + this.itmWldms + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMateriel(itmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生产物料信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportMateriel(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },

    handleAddCsm(row) {
      this.reset();

      getCs().then((response) => {
        if (response.data) {
          this.allCsmData = response.data;

          for (let i of this.allCsmData) {
            i.value = i.csmCsmc;
          }
        }
        this.title = "添加厂商";
        if (row) {
          this.itmMstr = row;
          this.csmForm.itcslWldm = row.itmWldm;
          this.csmForm.itmPmgg = row.itmPmgg;
          this.csmForm.itcslWldms = [row.itmWldm];
        } else {
          this.csmForm.itcslWldm = this.itmWldms.join(",");
          this.csmForm.itcslWldms = this.itmWldms;
          this.csmForm.itmPmgg = this.selection.map((o) => o.itmPmgg).join(",");
        }
        this.addCsmView = true;
      });
    },
    querySearch(queryString, cb) {
      this.csmForm.itcslCsdm = null;
      let restaurants = this.allCsmData;
      let results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return restaurant.csmCsmc
          .toLowerCase()
          .match(queryString.toLowerCase());
      };
    },

    submitCsmForm() {
      this.$refs["csmForm"].validate((valid) => {
        if (valid) {
          if (this.csmForm.itcslId != null) {
            updateCsm(this.csmForm).then((response) => {
              this.msgSuccess("更新成功");
              this.addCsmView = false;
              this.getList(this.itmData);
            });
          } else {
            bindCsm(this.csmForm).then((response) => {
              this.msgSuccess("绑定成功");
              this.addCsmView = false;
              this.getList(this.itmData);
            });
          }
        }
      });
    },

    handleSelect(data) {
      this.csmForm.itcslCsmc = data.map((item) => item.csmCsmc).join(",");
      this.csmForm.itcslCsdm = data.map((item) => item.csmCsdm).join(",");
    },
    proving(e) {
      let val = e.target.value;
      //限制只能输入一个小数点
      if (val.indexOf(".") != -1) {
        var str = val.substr(val.indexOf(".") + 1);
        if (str.indexOf(".") != -1) {
          val = val.substr(0, val.indexOf(".") + str.indexOf(".") + 1);
        }
      }
      e.target.value = val.replace(/[^\d^\.]+/g, "");
    },
    editInput(value, name) {
      this.csmForm[name] =
        ("" + value) // 第一步：转成字符串
          .replace(/[^\d^\.]+/g, "") // 第二步：把不是数字，不是小数点的过滤掉
          .replace(/^0+(\d)/, "$1") // 第三步：第一位0开头，0后面为数字，则过滤掉，取后面的数字
          .replace(/^\./, "0.") // 第四步：如果输入的第一位为小数点，则替换成 0. 实现自动补全
          .match(/^\d*(\.?\d{0,1})/g)[0] || ""; // 第五步：最终匹配得到结果 以数字开头，只有一个小数点，而且小数点后面只能有0到2位小数
    },

    getChildList(row) {
      //点击行触发，选中或不选中复选框
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
      this.itmData = row;
      if (row.itmWldm) {
        getCsmByWldm(row.itmWldm).then((res) => {
          this.csmList = res.data;
        });
      }
    },
    handleUpdateCsm(row) {
      getAllCsm().then((response) => {
        if (response.data) {
          this.allCsmData = response.data;

          for (let i of this.allCsmData) {
            i.value = i.csmCsmc;
          }
        }

        this.csmForm = {
          itcslId: row.itcslId,
          itcslCsdm: row.itcslCsdm,
          itcslCsmc: row.itcslCsmc,
          supplyQuota: row.supplyQuota,
          itcslWldm: this.itmData.itmWldm,
          itmPmgg: this.itmData.itmPmgg,
          remark: row.remark,
          itcslFlag: row.itcslFlag,
        };

        this.addCsmView = true;

        this.title = "修改厂商";
      });
    },

    handleDeleteCsm(row) {
      const itcslId = row.itcslId;
      const csmName = row.itcslCsmc;
      this.$confirm('是否确认删除厂商为"' + csmName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteCsm(itcslId);
        })
        .then(() => {
          this.getList(this.itmData);
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 角色状态修改
    handleStatusChange(row) {
      let text = row.itcslFlag === "0" ? "启用" : "停用";
      this.$confirm(
        '确认要"' + text + '"【' + row.itcslCsmc + "】厂商吗?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return updateItcs({ itcslId: row.itcslId, itcslFlag: row.itcslFlag });
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.itcslFlag = row.itcslFlag === "0" ? "1" : "0";
        });
    },
    editWorkshop() {
      this.workShop.form = this.selection[0];
      this.workShop.open = true;
    },
    submitWorkShop() {
      if (this.workShop.form.itmCj) {
        updateWorkShop(this.workShop.form).then((res) => {
          this.msgSuccess(res.msg);
          this.workShop.open = false;
        });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.unit-parent {
  height: 34px;
}

.unit {
  margin-left: 10px;
  height: 34px;
  line-height: 34px;
}

.unit-button {
  margin-left: 10px;
  height: 38px;
  line-height: 38px;
}

.el-table .cell {
  white-space: nowrap;
}
</style>
